package com.jinlia.show.modules.base.wxapp.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.constant.FissionTypeEnum;
import com.jinlia.show.modules.base.wxapp.entity.ActivityUserRel;
import com.jinlia.show.modules.base.wxapp.entity.Fission;
import com.jinlia.show.modules.base.wxapp.entity.User;
import com.jinlia.show.modules.base.wxapp.mapper.FissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class FissionMapperService extends ServiceImpl<FissionMapper, Fission> {

    @Autowired
    private UserMapperService userMapperService;
    @Autowired
    private ActivityUserRelMapperService activityUserRelMapperService;

    /**
     * 新增裂变记录
     *
     * @param fission
     * @return
     */
    @Transactional
    public Fission newFission(Fission fission) {
        Long countFission = this.lambdaQuery()
                .eq(Fission::getFissionType, fission.getFissionType())
                .eq(Fission::getChildOpenid, fission.getChildOpenid())
                .count();
        if (countFission > 0) {
            log.info("该用户已经参与过({})该裂变活动", fission.getFissionType());
            fission.setEnableStatus(EnableStatusEnum.FISSION_FAIL.getValue());
            fission.setRemark("该用户已经参与过该裂变活动");
            Fission one = this.lambdaQuery()
                    .eq(Fission::getFissionType, fission.getFissionType())
                    .eq(Fission::getChildOpenid, fission.getChildOpenid())
                    .eq(Fission::getParentOpenid, fission.getParentOpenid())
                    .one();
            if (one != null) {
                log.info("重复的邀请!!!");
                //已被邀请==>更新状态
                this.lambdaUpdate()
                        .eq(Fission::getId, one.getId())
                        .set(Fission::getLastUpdateTime, LocalDateTime.now())
                        .update();
                return one;
            }
        } else {
            log.info("该用户未参与({})该裂变活动", fission.getFissionType());
            //被裂变的用户
            User fissionUser = userMapperService.lambdaQuery()
                    .eq(User::getOpenid, fission.getChildOpenid())
                    .one();
            if (FissionTypeEnum.REGISTER.getValue().equals(fission.getFissionType())) {
                //注册裂变
                if (fissionUser.getCreateTime().plusDays(1).isBefore(LocalDateTime.now())) {
                    //1天内注册,不参与裂变
                    fission.setEnableStatus(EnableStatusEnum.FISSION_FAIL.getValue());
                    fission.setRemark(String.format("老用户,创建日期=%S", fission.getCreateTime()));
                }
            } else {
                //活动裂变==>判断用户有没有参加过注册裂变
                Fission oneFission = this.lambdaQuery()
                        .eq(Fission::getFissionType, FissionTypeEnum.REGISTER.getValue())
                        .eq(Fission::getChildOpenid, fission.getChildOpenid())
                        .one();
                //活动裂变
                if (oneFission == null && fissionUser.getCreateTime().plusDays(1).isAfter(LocalDateTime.now())) {
                    //新用户没有参与过祖册裂变
                    Fission fissionRegister = new Fission();
                    fissionRegister.setFissionType(FissionTypeEnum.REGISTER.getValue());
                    fissionRegister.setChildOpenid(fission.getChildOpenid());
                    fissionRegister.setParentOpenid(fission.getParentOpenid());
                    fissionRegister.setEnableStatus(EnableStatusEnum.FISSION_SUCCESS.getValue());
                    this.save(fissionRegister);
                    //裂变后TODO
                }
                //校验用户是否参与了该活动
                List<ActivityUserRel> list = activityUserRelMapperService.lambdaQuery()
                        .eq(ActivityUserRel::getActivityId, fission.getActivityId())
                        .eq(ActivityUserRel::getUserId, fission.getChildOpenid())
                        .list();
                if (!CollectionUtils.isEmpty(list)) {
                    log.info("用户已经参与过该活动,裂变失败");
                    fission.setEnableStatus(EnableStatusEnum.FISSION_FAIL.getValue());
                    fission.setRemark("用户已经参与过该活动");
                }
            }
        }
        this.save(fission);
        //裂变后TODO
        return fission;
    }

    public List<Fission> myFission(Fission fission) {
        List<Fission> list = this.lambdaQuery()
                .eq(Objects.nonNull(fission.getParentOpenid()), Fission::getParentOpenid, fission.getParentOpenid())
                .eq(Objects.nonNull(fission.getChildOpenid()), Fission::getChildOpenid, fission.getChildOpenid())
                .eq(Objects.nonNull(fission.getFissionType()), Fission::getFissionType, fission.getFissionType())
                .list();
        return list;
    }
}
